<?php

if (!defined('BASEPATH'))
    exit('No Direct Access Allowed !');

class admin_home extends CI_Controller {

    function __construct() {
        parent::__construct();
        $this->load->helper(array('url', 'form'));
    }

    function index() {
        $cek = $this->session->userdata('logged_in');
        if (empty($cek)) {
            header('location:' . site_url('admin'));
        } else {
            $this->load->model('m_jenis', 'm_siswa');
            $d['soal'] = $this->db->count_all('tb_soal');
            $d['jenis'] = $this->db->count_all('tb_jenis');
            $d['siswa'] = $this->db->count_all('tb_siswa');
            $this->load->view('admin/global/header');
            $this->load->view('admin/dashboard', $d);
            $this->load->view('admin/global/footer');
        }
    }

    function soal() {
        $cek = $this->session->userdata('logged_in');
        if (empty($cek)) {
            header('location:' . site_url('admin'));
        } else {
            $page = $this->uri->segment(3);
            $limit = 10;
            if (!$page):
                $offset = 0;
            else:
                $offset = $page;
            endif;
            $d['tot'] = $offset;
            $tot_hal = $this->db->get('tb_jenis');
            $config['base_url'] = site_url('admin_home/soal/');
            $config['total_rows'] = $tot_hal->num_rows();
            $config['per_page'] = $limit;
            $this->pagination->initialize($config);
            $d["paginator"] = $this->pagination->create_links();
            $this->load->model('m_jenis');
            $d['data_get'] = $this->m_jenis->ambil($limit, $offset);
            $this->load->view('admin/global/header');
            $this->load->view('admin/soal/home', $d);
            $this->load->view('admin/global/footer');
        }
    }

    /* form validation belum fix */

    function tambah_jenis() {
        $cek = $this->session->userdata('logged_in');
        if (empty($cek)) {
            header('location:' . site_url('admin'));
        } else {
            $d['kode_jenis'] = "";
            $d['nama_jenis'] = "";
            $d['versi_soal'] = "";
            $d['st'] = "Tambah";
            $d['id_jenis'] = "";
            $this->load->view('admin/global/header');
            $this->load->view('admin/soal/input_jenis', $d);
            $this->load->view('admin/global/footer');
        }
    }

    function edit() {
        $cek = $this->session->userdata('logged_in');
        if (empty($cek)) {
            header('location:' . site_url('admin'));
        } else {
            $id = $this->uri->segment(3);
            if ($id == NULL) {
                redirect('admin_home');
            }
            $this->load->model('m_jenis');
            $dt = $this->m_jenis->edit($id);
            $d['kode_jenis'] = $dt->kode_jenis;
            $d['nama_jenis'] = $dt->nama_jenis;
            $d['versi_soal'] = $dt->versi_soal;
            $d['st'] = "Edit";
            $d['id_jenis'] = $id;
            $this->load->view('admin/global/header');
            $this->load->view('admin/soal/input_jenis', $d);
            $this->load->view('admin/global/footer');
        }
    }

    function hapus() {
        $cek = $this->session->userdata('logged_in');
        if (empty($cek)) {
            header('location:' . site_url('admin'));
        } else {
            $u = $this->uri->segment(3);
            $this->load->model('m_jenis');
            $this->m_jenis->hapus($u);
            redirect('admin_home/soal');
        }
    }

    function simpan() {
        $cek = $this->session->userdata('logged_in');
        if ($cek) {
            $this->form_validation->set_rules('kd_soal', 'Kode Soal', 'trim|required');
            $this->form_validation->set_rules('nm_soal', 'Nama Soal', 'trim|required');
            $this->form_validation->set_rules('versi', 'Versi Soal', 'trim|required|is_numeric|is_natural');
            if ($this->form_validation->run() == FALSE) {
                if ($this->input->post('submit')) {
                    $d['kode_jenis'] = $this->input->post('kd_jenis');
                    $d['id_jenis'] = $this->input->post('id_jenis');
                    $d['st'] = $this->input->post('st');
                    $d['versi_soal'] = $this->input->post('versi');
                    $d['nama_jenis'] = $this->input->post('nm_soal');
                    $this->load->view('admin/global/header');
                    $this->load->view('admin/soal/input_jenis', $d);
                    $this->load->view('admin/global/footer');
                }
            } else {
                $st = $this->input->post('st');
                if ($this->input->post('submit')) {
                    if ($st == "Tambah") {
                        $this->load->model('m_jenis');
                        $this->m_jenis->tambah();
                        redirect('admin_home');
                    } else if ($st == "Edit") {
                        $id_jenis = $this->input->post('id');
                        $this->load->model('m_jenis');
                        $this->m_jenis->update($id_jenis);
                        redirect('admin_home');
                    }
                }
            }
        } else {
            redirect('admin_home');
        }
    }

    function cari() {
        $cek = $this->session->userdata('logged_in');
        if (empty($cek)) {
            header('location:' . site_url('admin'));
        } else {
            if ($this->input->post('cari') == "") {
                $kata = "";
            } else {
                $kata = $this->input->post('cari');
            }
            $this->load->model('m_jenis');
            $page = $this->uri->segment(4);
            $limit = 10;
            if (!$page):
                $offset = 0;
            else:
                $offset = $page;
            endif;
            $d['tot'] = $offset;
            $tot_hal = $this->m_jenis->cari_like($kata);
            $config['base_url'] = site_url('admin_home/index/');
            $config['total_rows'] = $tot_hal->num_rows();
            $config['per_page'] = $limit;
            $this->pagination->initialize($config);
            $d["paginator"] = $this->pagination->create_links();

            $d['data_get'] = $this->m_jenis->cari($kata, $limit, $offset);
            $this->load->view('admin/global/header');
            $this->load->view('admin/soal/home', $d);
            $this->load->view('admin/global/footer');
        }
    }

    function help() {
        $cek = $this->session->userdata('logged_in');
        if (empty($cek)) {
            header('location:' . site_url('admin'));
        } else {
            $this->load->view('admin/global/header');
            $this->load->view('admin/help');
            $this->load->view('admin/global/footer');
        }
    }

}

?>